<template>
  <el-dialog v-model="dialogVisible" class="!border-rd-10px" title="添加学校" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="学校名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入学校名称" />
      </el-form-item>
      <el-form-item label="所在地区" prop="school">
        <el-select
          v-model="ruleForm.school"
          filterable
          multiple
          collapse-tags
          collapse-tags-tooltip
          placeholder="可输入学校关键词"
        >
          <el-option label="222" value="1" />
          <el-option label="444" value="2" />
          <el-option label="2212" value="3" />
          <el-option label="113" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择组织">
        <el-select v-model="ruleForm.type" placeholder="请选择配送方式">
          <el-option label="统一配送到校" value="1" />
          <el-option label="京东快递" value="2" />
          <el-option label="已发货，只收款" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="学校logo">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="学校负责人">
        <el-input type="text" placeholder="请输入学校负责人" />
      </el-form-item>
      <el-form-item label="学校负责人电话">
        <el-input type="number" placeholder="请输入学校负责人电话" />
      </el-form-item>
      <el-form-item label="厂商负责人">
        <el-input type="text" placeholder="请输入厂商负责人" />
      </el-form-item>
      <el-form-item label="厂商负责人电话">
        <el-input type="number" placeholder="请输入厂商负责人电话" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { ComponentSize, FormInstance, FormRules , UploadProps } from 'element-plus'



const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
interface RuleForm {
  name: string
  school: null
  type: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  school: [],
  type: ''
})

const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: '请输入征订名称', trigger: 'blur' }],
  school: [
    {
      required: true,
      message: '请选择学校',
      trigger: 'change'
    }
  ],
  type: [
    {
      required: true,
      message: '请选择配送方式',
      trigger: 'change'
    }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log('submit!')
    } else {
      // console.log('error submit!', fields)
    }
  })
}

const dialogVisible = ref(false)
const titles = ref('添加学校')
function show(val,type) {
  dialogVisible.value = val
  titles.value = type === 'add' ? '添加学校' : '编辑学校'
}
defineExpose({ show, dialogVisible })
</script>
<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader{
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
}
.avatar-uploader .el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
